<?php
session_start();
ob_start();
include_once 'include/common.php';
global $core, $category_id;
$core = new core();
if (!$core->CheckLogin()) {
    header('Location:login.php');
}
$html_category = '';
$category_id = 0;
$product_id = isset($_GET["id"]) ? intval($_GET["id"]) : 0;
if ($product_id > 0) {
    $extra_photo_list = '';
    $extra_photo_color = '';
    $main_photo = '';
    $product = $core->getProductById($product_id);
    if (!$product) {
        header("location:productList.php");
    } else {
        $category_id = $product->product_category;
        $extra_photo_list = explode(",", $product->extra_photo);
        $extra_photo_color = explode(",", $product->product_color);
        $main_photo = $product->main_photo;
    }
}

$html_category = show_category(0,1,2);

function show_category($parent_id = 0,$current_level = 0, $max_level='') {

    global $core, $category_id;
    $html = '';
    $category = $core->getCategoryByParentId($parent_id);

    if ($category) {
        foreach ($category as $cate) {

            if($max_level > 0 && $current_level < $max_level) {
                $total_child_category = $core->getTotalChildCategoryById($cate->category_id);
            }else{
                $total_child_category = 0;
            }

            if ($total_child_category > 0) {

                $html.='<optgroup label="' . $cate->category_name . '">';
                $html.=show_category($cate->category_id,$current_level+1,$max_level);
                $html.='</optgroup>';
            } else {
                if ($category_id == $cate->category_id) {
                    $html.=' <option selected value="' . $cate->category_id . '">' . $cate->category_name . '</option>';
                } else {
                    $html.=' <option value="' . $cate->category_id . '">' . $cate->category_name . '</option>';
                }
            }
        }
    }
    return $html;
}
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <!--
                Charisma v1.0.0

                Copyright 2012 Muhammad Usman
                Licensed under the Apache License v2.0
                http://www.apache.org/licenses/LICENSE-2.0

                http://usman.it
                http://twitter.com/halalit_usman
        -->
        <meta charset="utf-8">
        <title>Zen store</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="Charisma, a fully featured, responsive, HTML5, Bootstrap admin template.">
        <meta name="author" content="Muhammad Usman">

        <!-- The styles -->
        <link id="bs-css" href="css/bootstrap-cerulean.css" rel="stylesheet">
        <style type="text/css">
            body {
                padding-bottom: 40px;
            }
            .sidebar-nav {
                padding: 9px 0;
            }
        </style>
        <link href="css/bootstrap-responsive.css" rel="stylesheet">
        <link href="css/charisma-app.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">
        <link href="css/jquery-ui-1.8.21.custom.css" rel="stylesheet">

        <link href='css/chosen.css' rel='stylesheet'>
        <link href='css/uniform.default.css' rel='stylesheet'>

        <link href='css/jquery.cleditor.css' rel='stylesheet'>


        <link href='css/jquery.iphone.toggle.css' rel='stylesheet'>

        <!-- CSS to style the file input field as button and adjust the Bootstrap progress bars -->
        <link rel="stylesheet" href="css/jquery.fileupload-ui.css">
        <!-- CSS adjustments for browsers with JavaScript disabled -->
        <noscript><link rel="stylesheet" href="css/jquery.fileupload-ui-noscript.css"></noscript>
        <link rel="shortcut icon" href="img/favicon.ico">
        <!-- blueimp Gallery styles -->
        <link rel="stylesheet" href='css/blueimp-gallery.min.css'>
      
    </head>

    <body>
        <div id="page-wrap" class="page-wrap body-wrap ">
            <div id="page-tray" class="tray transition">
                <?php include_once ('new_menu.php'); ?>

                <div id="page-content" class="page-content transition profile-page">
                    <?php include_once ('navbar.php'); ?>
                    <div class="container-fluid">
                        <div class="row-fluid">
                            <div id="content" class="span12">       
                                <!--                                <div>
                                                                    <ul class="breadcrumb">
                                                                        <li>
                                                                            <a href="#">Home</a> <span class="divider">/</span>
                                                                        </li>
                                                                        <li>
                                                                            <a href="#">Forms</a>
                                                                        </li>
                                                                    </ul>
                                                                </div>-->

                                <div class="row-fluid sortable">
                                    <div class="box span12">
                                        <div class="box-header well" data-original-title>
                                            <h2><i class="icon-edit"></i> Thêm sản phẩm</h2>
                                            <div class="box-icon">

                                                <a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>

                                            </div>
                                        </div>
                                        <div class="box-content">
                                            <div class="form-horizontal">
                                                <fieldset>
                                                    <div class="alert alert-error" id="alert-error-product_code" style="display:none"></div>
                                                    <div class="control-group">
                                                        <label class="control-label" for="product_code">Ref. * </label>
                                                        <div class="controls">

                                                            <div class="input-append">
                                                                <input type="text" size="16" name="product_code" id="product_code" value="<?php if ($product_id > 0) echo $product->product_code; ?>">
                                                                <button type="button" class="btn" id="get_product_info_by_code">Check Ref</button>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="control-group">
                                                        <label class="control-label" for="product_name">Tên sản phẩm </label>
                                                        <div class="controls">
                                                            <input type="text" class="span6 typeahead" id="product_name" value="<?php if ($product_id > 0) echo $product->product_name; ?>"  >

                                                        </div>
                                                    </div>

                                                    <div class="control-group">
                                                        <label class="control-label" for="link_web">Link web</label>
                                                        <div class="controls">
                                                            <input type="text" class="span6 typeahead" id="link_web" value="<?php if ($product_id > 0) echo $product->link_web; ?>"  >

                                                        </div>
                                                    </div>
                                                    <div class="control-group">
                                                        <label class="control-label" for="product_price">Giá bán</label>
                                                        <div class="controls">
                                                            <div class="input-prepend input-append">
                                                                <input type="text" class="span6 typeahead" id="product_price" value="<?php if ($product_id > 0) echo $product->product_price / 1000; ?>" >
                                                                <span class="add-on">000 VNĐ</span>
                                                            </div>

                                                        </div>
                                                    </div>

                                                    <div class="control-group">
                                                        <label class="control-label" for="store_price">Giá PT</label>
                                                        <div class="controls">
                                                            <input type="text" class="span6 typeahead" id="store_price" value="<?php if ($product_id > 0) echo number_format ($product->store_price,2); ?>" >

                                                        </div>
                                                    </div>
                                                    <div class="control-group">
                                                        <label class="control-label" for="stamp_price">Giá AT</label>
                                                        <div class="controls">
                                                            <input type="text" class="span6 typeahead" id="stamp_price" value="<?php if ($product_id > 0) echo number_format($product->stamp_price,2); ?>"  >

                                                        </div>
                                                    </div>
                                                    <div class="control-group">
                                                        <label class="control-label" for="product_size">Size</label>

                                                        <div class="controls">
                                                            <input type="text" class="span6 typeahead" id="product_size"  value="<?php if ($product_id > 0) echo $product->product_size; ?>" >
                                                            <span class="help-inline">Cách nhau bởi dấu phẩy: M,L,XL</span>
                                                        </div>
                                                    </div>


                                                    <div class="control-group">
                                                        <label class="control-label" for="product_category">Category*</label>
                                                        <div class="controls">
                                                            <select  id="product_category" >

                                                                <?php echo $html_category; ?>

                                                            </select>
                                                        </div>
                                                    </div>
                                                    <div class="control-group">
                                                        <label class="control-label" for="fileInput">Chọn ảnh sản phẩm*</label>
                                                        <div class="controls">
                                                            <input type="hidden" name="folder_temp" id="folder_temp" value="<?php if ($product_id > 0) echo "photo"; ?>">
                                                            <label class="radio">
                                                                <div class="radio">
                                                                    <span class="checked">
                                                                        <input type="radio" value="0" name="upload_photo" style="opacity: 0;">
                                                                    </span>
                                                                </div>
                                                                Upload ảnh từ link
                                                            </label>
                                                            <label class="radio">
                                                                <div class="radio">
                                                                    <span class="checked">
                                                                        <input type="radio" value="1" name="upload_photo" style="opacity: 0;">
                                                                    </span>
                                                                </div>
                                                                Upload ảnh từ máy tính
                                                            </label>
                                                            <p></p>
                                                            <div class="input-append upload_photo_from_url hide" style="margin-bottom: 10px">
                                                                <input type="text"  id="url_main_photo" value="Điền link tải ảnh">
                                                                <button type="button" class="btn" id="btn_upload_main_photo">upload photo</button>
                                                            </div>

                                                            <form class="form-horizontal form_upload_photo hide" name="form_upload_photo" id="form_upload_photo" method="post" enctype="multipart/form-data" action="ajax/uploadProductPhoto.php" target="iframe_upload_photo">
                                                                <input class="input-file uniform_on" id="product_photo" type="file" name="product_photo[]" multiple="multiple">

                                                            </form>
                                                            <iframe class='hide' id="iframe_upload_photo" name="iframe_upload_photo" src=""></iframe>

                                                            <?php
                                                            if ($product_id > 0) {
                                                                ?>
                                                                <table class="table table-striped " id="table_upload_photo_list">
                                                                    <tr><th>Ảnh đại diện</th><th>photo</th><th>Màu sản phẩm</th><th></th></tr>
                                                                    <?php
                                                                    $i = 0;
                                                                    foreach ($extra_photo_list as $extra_photo_name) {
                                                                        echo '<tr class="tr_product_photo">';
                                                                        if ($main_photo == $extra_photo_name) {
                                                                            echo '<td>
                                                                                    <label class="radio">
                                                                                        <div class="radio">
                                                                                            <span class="checked">
                                                                                                <input class="is_main_photo" type="radio" name="is_main_photo" value="' . $i . '" style="opacity: 0;" checked>
                                                                                            </span>
                                                                                        </div>
                                                                                    </label>
                                                                                </td>';
                                                                        } else {
                                                                            echo '<td>
                                                                                    <label class="radio">
                                                                                        <div class="radio">
                                                                                            <span class="">
                                                                                                <input class="is_main_photo" type="radio" name="is_main_photo" value="' . $i . '" style="opacity: 0;" >
                                                                                            </span>
                                                                                        </div>
                                                                                    </label>
                                                                                </td>';
                                                                        }

                                                                        echo '<td><a href="#"><img src="photo/extra_photo/thumb_extra_photo/' . $extra_photo_name . '"></a>
                                                                                    <input type="hidden" value="'. $extra_photo_name .'" class="array_photo_list"/>
                                                                              </td>
                                                                                        <td><input class="color_name" type="text" name="color_name" value="' . $extra_photo_color[$i] . '"></td>
                                                                                        <td><a href="javascript:void(0)" class="btn btn-danger delete_product_photo" ><i class="icon-trash icon-white"></i>Delete</a></td>
                                                                                         </tr>';

                                                                        $i++;
                                                                    }
                                                                    ?>

                                                                </table>
                                                                <?php
                                                            } else {
                                                                ?>
                                                                <table class="table table-striped hide" id="table_upload_photo_list">
                                                                    <tr><th>Ảnh đại diện</th><th>photo</th><th>Màu sản phẩm</th><th></th></tr>
                                                                </table> 
                                                                <?php
                                                            }
                                                            ?>
                                                            <div id="show_error_upload"></div>
                                                        </div>
                                                    </div>    

                                                    <div class="control-group">
                                                        <label class="control-label" for="description">Miêu tả sản phẩm</label>
                                                        <div class="controls">
                                                            <textarea class="cleditor" id="description" rows="3"><?php if ($product_id > 0) echo $product->description; ?></textarea>
                                                        </div>
                                                    </div>

                                                    <div class="alert alert-error" id="alert-error-product" style="display:none">
                                                    </div>
                                                    <div class="form-actions">
                                                        <input type="hidden" name="product_id" id="product_id" value="<?php if ($product_id > 0) echo $product_id; ?>">
                                                        <button type="button" class="btn btn-primary" id="save_product">Save changes</button>
                                                        <button type="button" class="btn" id="reset_form">Cancel</button>
                                                    </div>
                                                </fieldset>
                                            </div>   

                                        </div>
                                    </div><!--/span-->

                                </div><!--/row-->


                                <div class="row-fluid sortable">
                                    <div class="box span12">
                                        <div class="box-header well" data-original-title>
                                            <h2><i class="icon-edit"></i> Import sản phẩm</h2>
                                            <div class="box-icon">
                                                <a href="#" class="btn btn-setting btn-round"><i class="icon-cog"></i></a>
                                                <a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
                                                <a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
                                            </div>
                                        </div>
                                        <div class="box-content">
                                            <form class="form-horizontal form_upload_file" name="form_upload_file" id="form_upload_file" method="post" enctype="multipart/form-data" action="ajax/uploadFileProduct.php" target="upload_target">
                                                <fieldset>
                                                    <div class="control-group">
                                                        <label class="control-label" for="fileInput">File input</label>
                                                        <div class="controls upload-btn">
                                                            <input class="input-file uniform_on" id="input_upload_file" type="file" name="file">
                                                        </div>
                                                    </div> 
                                                    <div class="form-actions">
                                                        <button type="submit" class="btn btn-primary">Save changes</button>
                                                        <button class="btn">Cancel</button>
                                                    </div>
                                                </fieldset>
                                            </form>
                                            <iframe id="upload_target" name="upload_target" src=""></iframe>

                                        </div>
                                    </div><!--/span-->

                                </div><!--/row-->


                            </div><!--/fluid-row-->
                            <?php include_once ('footer.php'); ?>

                        </div><!--/.fluid-container-->
                    </div>
                </div><!--/#page-content-->
            </div>
        </div>
        <!-- external javascript
        ================================================== -->
        <!-- Placed at the end of the document so the pages load faster -->

        <!-- jQuery -->
        <script src="js/jquery-1.7.2.min.js"></script>
        <!-- jQuery UI -->
        <script src="js/jquery-ui-1.8.21.custom.min.js"></script>
        <!-- transition / effect library -->
        <script src="js/bootstrap-transition.js"></script>
        <!-- alert enhancer library -->
        <script src="js/bootstrap-alert.js"></script>
        <!-- accordion library (optional, not used in demo) -->
        <script src="js/bootstrap-collapse.js"></script>
        <!-- library for creating tabs -->
        <script src="js/bootstrap-tab.js"></script>
        <!-- library for advanced tooltip -->
        <script src="js/bootstrap-tooltip.js"></script>
        <!-- popover effect library -->
        <script src="js/bootstrap-popover.js"></script>

        <!-- library for cookie management -->
        <script src="js/jquery.cookie.js"></script>


        <!-- select or dropdown enhancer -->
        <script src="js/jquery.chosen.min.js"></script>
        <!-- checkbox, radio, and file input styler -->
        <script src="js/jquery.uniform.min.js"></script>

        <!-- rich text editor library -->
        <script src="js/jquery.cleditor.min.js"></script>

        <!-- for iOS style toggle switch -->
        <script src="js/jquery.iphone.toggle.js"></script>
        <!-- autogrowing textarea plugin -->
        <script src="js/jquery.autogrow-textarea.js"></script>
        <!-- multiple file upload plugin -->

        <!-- history.js for cross-browser state change on ajax -->
        <script src="js/jquery.history.js"></script>
        <!-- application script for Charisma demo -->
        <script src="js/common.js?v=1"></script>
        <script src="js/addProduct.js?t=9"></script>
    </body>
</html>

